<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Syntax error</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Syntax_error"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Syntax_error rootpage-Syntax_error skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Syntax error</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">For the 2003 Australian film, see <a href="Syntax_Error_(film)" title="Syntax Error (film)">Syntax Error (film)</a>.</div>
<p>A <b>syntax error</b> is a mismatch in the <a href="Syntax_(programming_languages)" title="Syntax (programming languages)">syntax</a> of <a href="Data" title="Data">data</a> <a href="Input_data" class="mw-redirect" title="Input data">input</a> to a <a href="Computer_system" class="mw-redirect" title="Computer system">computer system</a> that requires a specific syntax. For <a href="Source_code" title="Source code">source code</a> in a <a href="Programming_language" title="Programming language">programming language</a>, a <a href="Compiler" title="Compiler">compiler</a> detects syntax errors before the software is run; at compile-time, whereas an <a href="Interpreter" class="mw-redirect" title="Interpreter">interpreter</a> detects syntax errors at <a href="Run_time_(program_lifecycle_phase)" class="mw-redirect" title="Run time (program lifecycle phase)">run-time</a>. A syntax error can occur based on syntax rules other than those defined by a programming language. For example, typing an invalid equation into a calculator (an interpreter) is a syntax error.
</p><p>Some errors that occur during the translation of source code may be considered syntax errors by some but not by others. For example, some say that an uninitialized variable in <a href="Java_(programming_language)" title="Java (programming language)">Java</a> is a syntax error, but others disagree<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-uninitialized_var_2-0" class="reference"><a href="#cite_note-uninitialized_var-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> – classifying it as a <a href="Programming_language#Static_semantics" title="Programming language">static semantic</a> error.<sup id="cite_ref-uninitialized_var_2-1" class="reference"><a href="#cite_note-uninitialized_var-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup>
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Examples">Examples</h2></div>
<div class="mw-heading mw-heading3"><h3 id="In_Java">In Java</h3></div>
<p>The Java compiler generates a syntax error for the following code since the string is not quoted. The compilation process fails and does not produce a usable executable.
</p>
<pre>System.out.println(Hello World);</pre>
<p>Valid syntax is:
</p><p><syntax highlighting lang="java">
System.out.println("Hello World");
</syntax highlighting
>
</p>
<div class="mw-heading mw-heading3"><h3 id="In_Lisp">In Lisp</h3></div>
<p>The code <code>(add 1 1)</code> is a syntactically valid <a href="Lisp_(programming_language)" title="Lisp (programming language)">Lisp</a> program (assuming the 'add' function exists) that adds 1 and 1.
</p><p>However, <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">(_ 1 1)</code> results in syntax error <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">lexical error: '_' is not valid</code>. The lexer is unable to identify the first error – all it knows is that, after producing the token LEFT_PAREN, '(' the remainder of the program is invalid, since no word rule begins with '_'. And, <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">(add 1 1</code> results in syntax error <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">parsing error: missing closing ')'</code>. The parser identifies the "list" production rule due to the '(' token (as the only match), and thus gives an error message; in general it may be <a href="Ambiguous_grammar" title="Ambiguous grammar">ambiguous grammar</a>.
</p><p>Type errors and undeclared variable errors are sometimes considered to be syntax errors when they are detected at compile-time (which is usually the case when compiling strongly-typed languages), though it is common to classify these kinds of error as <a href="Programming_language#Semantics" title="Programming language">semantic</a> errors instead.<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-uninitialized_var_2-2" class="reference"><a href="#cite_note-uninitialized_var-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="In_Python">In Python</h3></div>
<p>For <a href="Python_(programming_language)" title="Python (programming language)">Python</a> code, <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">'a' + 1</code> contains a type error because it adds a string literal to an integer literal. A type error like this can be detected at compile-time – during parsing (phrase analysis) – if the compiler uses separate rules that allow "<i>integer-literal</i> + <i>integer-literal</i>" but not "<i>string-literal</i> + <i>integer-literal</i>", though it is more likely that the compiler will use a parsing rule that allows expressions of the form "<i>literal-or-identifier</i> + <i>literal-or-identifier</i>" and then the error will be detected during contextual analysis (when type checking occurs). In some cases, this validation is not done by the compiler, and these errors are only detected at runtime.
</p><p>In a dynamically typed language, where type can only be determined at runtime, many type errors can only be detected at runtime. For example, for Python <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">a + b</code> is syntactically valid at the phrase level, but the correctness of the types of a and b can only be determined at runtime, as variables do not have types in Python, only values do. Whereas there is disagreement about whether a type error detected by the compiler should be called a syntax error (static semantic), type errors which can only be detected at program execution time are always regarded as semantic rather than syntax errors.
</p>
<div class="mw-heading mw-heading3"><h3 id="On_a_calculator">On a calculator</h3></div>
<p>A syntax error can occur on a <a href="Calculator" title="Calculator">calculator</a> (especially a <a href="Scientific_calculator" title="Scientific calculator">scientific</a> or <a href="Graphing_calculator" title="Graphing calculator">graphing</a> calculator) when the input <a href="Equation" title="Equation">equation</a> is incorrect in ways such as:
</p>
<ul><li>Invalid number or operation</li>
<li>Open <a href="Bracket#usage_in_mathematics" title="Bracket">bracket</a> without closing</li>
<li>Using <a href="Minus_sign" class="mw-redirect" title="Minus sign">minus sign</a> instead of negative symbol (or vice versa)</li></ul>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Tag_soup" title="Tag soup">Tag soup</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://stackoverflow.com/questions/8803718/issue-of-syntax-or-semantics/8803765#8803765">Issue of syntax or semantics?</a></span>
</li>
<li id="cite_note-uninitialized_var-2"><span class="mw-cite-backlink">^ <a href="#cite_ref-uninitialized_var_2-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-uninitialized_var_2-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-uninitialized_var_2-2"><sup><i><b>c</b></i></sup></a></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.dummies.com/how-to/content/semantic-errors-in-java.html">Semantic Errors in Java</a></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFAhoMonica_S._LamRavi_SethiJeffrey_D._Ullman2007" class="citation book cs1">Aho, Alfred V.; Monica S. Lam; Ravi Sethi; Jeffrey D. Ullman (2007). <span class="id-lock-registration" title="Free registration required"><a rel="nofollow" class="external text" href="https://archive.org/details/compilers00alfr_0"><i>Compilers: Principles, Techniques, and Tools</i></a></span> (2nd ed.). Addison Wesley. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0-321-48681-3</bdi>.</cite> Section 4.1.3: Syntax Error Handling, pp.194–195.</span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite id="CITEREFLouden1997" class="citation book cs1">Louden, Kenneth C. (1997). <i>Compiler Construction: Principles and Practice</i>. Brooks/Cole. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>981-243-694-4</bdi>.</cite> Exercise 1.3, pp.27–28.</span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><cite id="CITEREFAhoMonica_S._LamRavi_SethiJeffrey_D._Ullman2007" class="citation book cs1">Aho, Alfred V.; Monica S. Lam; Ravi Sethi; Jeffrey D. Ullman (2007). <a rel="nofollow" class="external text" href="https://archive.org/details/compilers00alfr_0"><i>Compilers: Principles, Techniques, and Tools</i></a> (2nd ed.). Addison Wesley. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0-321-48681-3</bdi>.</cite>Section 4.1.3: Syntax Error Handling, pp.194–195.</span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><cite id="CITEREFLouden1997" class="citation book cs1">Louden, Kenneth C. (1997). <i>Compiler Construction: Principles and Practice</i>. Brooks/Cole. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>981-243-694-4</bdi>.</cite> Exercise 1.3, pp.27–28.</span>
</li>
</ol></div></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-07-29" href="https://en.wikipedia.org/wiki/?title=Syntax_error&oldid=1303121379">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>